package com.chpro.orm.hibernate;

import java.nio.charset.Charset;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chpro.excel.ExcelUtil;

public abstract class ControllerBaseImpl<T>  implements
ControllerBase<T> {
	
	public abstract ServiceBase<T> getDao();

	@Override
	@RequestMapping(value="save.action")
	public @ResponseBody
	String save(T obj) {
		getDao().save(obj);
		return "success";
	}

	@Override
	@RequestMapping(value="update.action")
	public @ResponseBody
	String update(T obj) {
		getDao().update(obj);
		return "success";
	}

	@Override
	@RequestMapping(value="delete.action")
	public @ResponseBody
	String delete(String id) {
		getDao().delete(id);
		return "success";
	}

	@Override
	@RequestMapping(value="findById.action")
	public @ResponseBody
	T findById(String id) {
		T t = getDao().findById(id);
		return t;
	}

	@Override
	public @ResponseBody
	String deleteByIdList(Integer[] idList) {
		getDao().deleteByIdList(idList);
		return "success";
	}

	@Override
	@RequestMapping(value="findParasByPageToJSON.action")
	public ResponseEntity<String> findParasByPageToJSON(String page,String rows) {
		String result = getDao().findByPageToJSON(Integer.parseInt(page),Integer.parseInt(rows));
		
		//解决JSON返回中文乱码问题
		HttpHeaders headers = new HttpHeaders();      
        MediaType mediaType=new MediaType("text","html",Charset.forName("GBK"));      
        headers.setContentType(mediaType);      
        ResponseEntity<String> responseEntity =new ResponseEntity<String>(result,headers,HttpStatus.OK);      
        return responseEntity;
	}

	@Override
	@RequestMapping(value="dataOper.action")
	public @ResponseBody
	String dataOper(T obj, String oper) {
		//System.out.println(JSONUtil.writeEntityJSON(obj)+" "+oper);
		String result = "success";
		if("add".equals(oper)){
			getDao().save(obj);
			return result;
		}else if("edit".equals(oper)){
			getDao().update(obj);
			return result;
		}else if("del".equals(oper)){
			getDao().delete(obj);
			return result;
		}
		return "error";
	}

	@Override
	@RequestMapping(value="excelExport.action")
	public String excelExport(HttpServletRequest request,HttpServletResponse response)throws Exception {
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		response.setHeader("Content-disposition","attachment; filename="+(new Date()));
		getDao().excelExport(response.getOutputStream());
		return null;
	}

	@Override
	@RequestMapping(value="pdfExport.action")
	public String pdfExport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/pdf;charset=UTF-8");
		response.setHeader("Content-disposition","attachment; filename="+(new Date())+".pdf");
		getDao().pdfExport(response.getOutputStream());
		return null;
	}	
	
	
	
}
